package adfc2.render3d.mesh;

import adfc2.core.Material;
import adfc2.render3d.TextureReference;
import adfc2.render3d.tex.TiledTexture;
import adfc2.util.Lexer;

import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * Класс hard-coded моделей игры
 */
public class Meshes {
    private Map<String, Mesh> loadedMeshes;
    private final TiledTexture materials;
    private final TiledTexture items;

    private Mesh safeLoad(String code) {
        Mesh m = loadedMeshes.get(code);
        return (m == null) ? new Mesh() : m;
    }

    public Meshes(TiledTexture materials, TiledTexture items) {
        this.materials = materials;
        this.items = items;

        try {
            loadedMeshes = new MeshLoader(new Lexer(new FileReader("meshes.txt"))).loadMeshes();
        } catch (IOException e) {
            e.printStackTrace(); // TODO log
            loadedMeshes = new HashMap<String, Mesh>();
        }

        TextureReference granite = materials.tiles[Material.GRANITE.ordinal()];
        MASON_WORKSHOP_MESH = safeLoad("MASON_WORKSHOP_MESH");
        TREE_MESH = safeLoad("TREE_MESH");
        DROPPED_ITEM_MESH = safeLoad("DROPPED_ITEM_MESH");
        GLYPH_AT = safeLoad("GLYPH_AT");
    }

    public final Mesh MASON_WORKSHOP_MESH;
    public final Mesh TREE_MESH;
    public final Mesh DROPPED_ITEM_MESH;
    public final Mesh GLYPH_AT;
}
